package com.franklin.java.configuration.sql.run;

import com.franklin.java.api.configuration.AbstractProjectConfigurable;
import com.franklin.java.configuration.sql.run.ui.JdbcConsoleComponent;
import com.intellij.openapi.options.ConfigurationException;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.NlsContexts;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;

/**
 * JdbcConsole配置
 * @author Franklin
 * @since 2021/9/26 16:59
 */
public class JdbcConsoleConfigurable extends AbstractProjectConfigurable {

    private JdbcConsoleComponent jdbcConsoleComponent;

    public JdbcConsoleConfigurable(Project myProject) {
        super(myProject);
    }

    @Override
    public String getDisplayName() {
        return "Mybatis Sql Execute Console";
    }

    @Nullable
    @Override
    public JComponent createComponent() {
        jdbcConsoleComponent = new JdbcConsoleComponent(myProject);
        return jdbcConsoleComponent.getMainPanel();
    }

    @Override
    public boolean isModified() {
        return !jdbcConsoleComponent.getSelectedJdbcConsole().equals(JdbcConsoleState.getConsole(myProject));
    }

    @Override
    public void apply() throws ConfigurationException {
        JdbcConsoleState jdbcConsoleState = JdbcConsoleState.getInstance(myProject);
        jdbcConsoleState.console = jdbcConsoleComponent.getSelectedJdbcConsole();
    }
}
